home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / TERMINAL / SRCS / PROCEDUR.C < prev    next >
Text File  |  1990-11-20  |  9KB  |  362 lines

  1. /*
  2.     Terminal 2.0
  3.     "Procedure.c"
  4. */
  5.  
  6. #ifdef THINK_C
  7. #include "MacHeaders"
  8. #endif
  9. #ifdef applec
  10. #pragma load ":(Objects):MacHeadersMPW"
  11. #pragma segment Main2
  12. #endif
  13.  
  14. #include "Procedure.h"
  15. #include "Text.h"
  16. #include "Main.h"
  17. #include "File.h"
  18. #include "Serial.h"
  19. #include "Document.h"
  20. #include "Utilities.h"
  21. #include "XModem.h"
  22. #include "ZModem.h"
  23.  
  24. /* ----- Send string --------------------------------------------------- */
  25.  
  26. short Type(register Byte *s)
  27. {
  28.     register short err;
  29.     register Byte *p;
  30.     register long count;
  31.     register short n;
  32.     Byte buffer[256];
  33.  
  34.     if (Sending)
  35.         return CANCEL;
  36.     Control_X = FALSE;
  37.     /* SerialHandshake(Settings.handshake); */
  38.     while (Busy)
  39.         ;
  40.     if (Settings.autoLF) {
  41.         n = *(Byte *)s++;    /* Length of pascal string */
  42.         p = (Byte *)s; 
  43.         while (n--) {
  44.             if (*s++ == '\015') {    /* CR */
  45.                 memcpy(buffer, p, count = (Byte *)s - p);
  46.                 buffer[count++] = '\012';    /* LF */
  47.                 Sending = TRUE;
  48.                 Control_X = FALSE;
  49.                 SerialSend(buffer, count, &Busy);
  50.                 if (Settings.localEcho)
  51.                     NewCharacters(buffer, count, FALSE);
  52.                 CheckEvents();
  53.                 while (Busy)
  54.                     ;
  55.                 if (!Sending) {        /* CAN received */
  56.                     err = Control_X ? ABORT : CANCEL;
  57.                     goto done;
  58.                 }
  59.                 p = (Byte *)s;
  60.             }
  61.         }
  62.         count = (Byte *)s - p;
  63.     } else {
  64.         count = *s++;    /* Length of pascal string */
  65.         p = (Byte *)s;
  66.     }
  67.     if (count) {
  68.         Sending = TRUE;
  69.         Control_X = FALSE;
  70.         SerialSend(p, count, &Busy);
  71.         if (Settings.localEcho)
  72.             NewCharacters(p, count, FALSE);
  73.         CheckEvents();
  74.         while(Busy)
  75.             ;
  76.         if (!Sending) {        /* CAN received */
  77.             err = Control_X ? ABORT : CANCEL;
  78.             goto done;
  79.         }
  80.     }
  81.     err = FINE;
  82. done:
  83.     /* SerialHandshake(0); */
  84.     Sending = FALSE;
  85.     Control_X = FALSE;
  86.     return err;
  87. }
  88.  
  89. /* ----- Display character string in terminal window ------------------- */
  90.  
  91. short Display(register Byte *s)
  92. {
  93.     NewCharacters((Byte *)s + 1, *s, FALSE);
  94.     return FINE;
  95. }
  96.  
  97. /* ----- Change communication settings --------------------------------- */
  98.  
  99. short PortSetUp(
  100.     short baud,        /*    0=300, 1=600, 2=1200, 3=2400, 4=4800,
  101.                         5=9600, 6=19200, 7=38400, 8=57600 */
  102.     short data,        /* 0=7, 1=8 data bits */
  103.     short parity,    /* 0=noParity, 1=evenParity, 2=oddParity */
  104.     short stop,        /* 0=1, 1=2 stop bit */
  105.     Byte *port,        /* Port name */
  106.     short dtr,        /* 1=don't drop DTR when quitting */
  107.     short hs)        /* 0=none, 1=XON/XOFF, 2=CTS, 3=DTR, 4=CTS/DTR */
  108. {
  109.     register short err;
  110.     short b, d, p, s;
  111.     short setup;
  112.  
  113.     SerialGetSetup(Settings.portSetup, &b, &d, &p, &s);
  114.     if (baud < 0 || baud > 8)
  115.         baud = b;        /* No change */
  116.     if (data < 0 || data > 1)
  117.         data = d;        /* No change */
  118.     if (parity < 0 || parity > 2)
  119.         parity = p;        /* No change */
  120.     if (stop < 0 || stop > 1)
  121.         stop = s;        /* No change */
  122.     SerialSetSetup(baud, data, parity, stop, &setup);
  123.     if (hs < 0 || hs > 4)
  124.         hs = Settings.handshake;
  125.     if (!port)            /* No change */
  126.         port = Settings.portName;
  127.     if (setup != Settings.portSetup ||
  128.             !EqualString(port, Settings.portName, FALSE, TRUE) ||
  129.             hs != Settings.handshake) {
  130.         Settings.portSetup = setup;
  131.         Settings.handshake = hs;
  132.         Settings.dirty = TRUE;
  133.         SerialClose();
  134.         if (err = SerialOpen(port,Settings.portSetup,Settings.handshake))
  135.             return err;
  136.     }
  137.     if ((dtr >= 0 && dtr <= 1) && dtr != Settings.dropDTR) {
  138.         Settings.dropDTR = dtr;
  139.         Settings.dirty = TRUE;
  140.     }
  141.     return noErr;
  142. }
  143.  
  144. /* ----- Get volume/directory for up- and downloads -------------------- */
  145.  
  146. short Folder(
  147.     register short *volume,
  148.     register long *directory)
  149. {
  150.     *volume = Settings.volume;
  151.     *directory = Settings.directory;
  152.     return FINE;
  153. }
  154.  
  155. /* ----- Set echo modes and autoLF ------------------------------------- */
  156.  
  157. short TerminalSetup(
  158.     register short lEcho,
  159.     register short rEcho,
  160.     register short autoLF,
  161.     register short save)
  162. {
  163.     if (lEcho >= 0 && lEcho <= 1 && lEcho != Settings.localEcho) {
  164.         Settings.localEcho = lEcho;
  165.         Settings.dirty = TRUE;
  166.     }
  167.     if (rEcho >= 0 && rEcho <= 1 && rEcho != Settings.echo) {
  168.         Settings.echo = rEcho;
  169.         Settings.dirty = TRUE;
  170.     }
  171.     if (autoLF >= 0 && autoLF <= 1 && autoLF != Settings.autoLF) {
  172.         Settings.autoLF = autoLF;
  173.         Settings.dirty = TRUE;
  174.     }
  175.     if (save >= 0 && save <= 1 && save != Settings.save) {
  176.         Settings.save = save;
  177.         Settings.dirty = TRUE;
  178.     }
  179.     return FINE;
  180. }
  181.  
  182. /* ----- Set binary file transfer options ------------------------------ */
  183.  
  184. short TransferSetup(
  185.     Boolean binary,
  186.     Boolean cis,
  187.     Boolean zmodem,
  188.     Boolean zautorx)
  189. {
  190.     if (binary >= 0 && binary <= 1 && binary != Settings.Binary) {
  191.         Settings.Binary = binary;
  192.         Settings.dirty = TRUE;
  193.     }
  194.     if (cis >= 0 && cis <= 1 && cis != Settings.protocol) {
  195.         Settings.protocol = cis;
  196.         Settings.dirty = TRUE;
  197.     }
  198.     if (zmodem >= 0 && zmodem <= 1 && zmodem != Settings.ZModem) {
  199.         Settings.ZModem = zmodem;
  200.         Settings.dirty = TRUE;
  201.     }
  202.     if (zautorx >= 0 && zautorx <= 1 && zautorx != Settings.ZAutoReceive) {
  203.         Settings.ZAutoReceive = zautorx;
  204.         Settings.dirty = TRUE;
  205.     }
  206.     return FINE;
  207. }
  208.  
  209. /* ----- Set XYModem options ------------------------------------------- */
  210.  
  211. short XYModemSetup(
  212.     Boolean crc,
  213.     short block,
  214.     short batch,
  215.     long timeout)
  216. {
  217.     if (crc >= 0 && crc <= 1 && crc != Settings.XModemCRC) {
  218.         Settings.XModemCRC = crc;
  219.         Settings.dirty = TRUE;
  220.         if (!crc)
  221.             Settings.XModem1K = 0;
  222.     }
  223.     if (block >= 0 && block <= 2 && block != Settings.XModem1K) {
  224.         Settings.XModem1K = block;
  225.         Settings.dirty = TRUE;
  226.     }
  227.     if (batch >= 0 && batch <= 2 && batch != Settings.batch) {
  228.         Settings.batch = batch;
  229.         Settings.dirty = TRUE;
  230.     }
  231.     if (timeout >= 0 && timeout != Settings.XModemtimeout) {
  232.         Settings.XModemtimeout = timeout;
  233.         Settings.dirty = TRUE;
  234.     }
  235.     return FINE;
  236. }
  237.  
  238. /* ----- Set ZModem options -------------------------------------------- */
  239.  
  240. short ZModemSetup(
  241.     Boolean escctl,
  242.     long timeout,
  243.     long retries,
  244.     long buffer,
  245.     long packet,
  246.     long window,
  247.     long crcq)
  248. {
  249.     if (escctl >= 0 && escctl <= 1 && escctl != Settings.ZEscapeCtl) {
  250.         Settings.ZEscapeCtl = escctl;
  251.     }
  252.     if (timeout >= 0 && timeout != Settings.ZTimeout) {
  253.         Settings.ZTimeout = timeout;
  254.         Settings.dirty = TRUE;
  255.     }
  256.     if (buffer >= 0 && buffer <= 0x7FFF && buffer != Settings.ZBuffer) {
  257.         Settings.ZBuffer = buffer;
  258.         Settings.dirty = TRUE;
  259.     }
  260.     if (retries >= 3 && retries <= 30 && retries != Settings.ZRetries) {
  261.         Settings.ZRetries = retries;
  262.         Settings.dirty = TRUE;
  263.     }
  264.     if (packet >= 128 && packet <= 1024 && packet != Settings.ZPacket) {
  265.         Settings.ZPacket = packet;
  266.         Settings.dirty = TRUE;
  267.     }
  268.     if (window >= 0 && window <= 0x7FFF && window != Settings.ZWindow) {
  269.         Settings.ZWindow = window;
  270.         Settings.dirty = TRUE;
  271.     }
  272.     if (crcq >= 0 && crcq <= 0x7FFF && crcq != Settings.Zcrcq) {
  273.         Settings.Zcrcq = crcq;
  274.         Settings.dirty = TRUE;
  275.     }
  276.     return FINE;
  277. }
  278.  
  279. /* ----- Text file send parameters setup ------------------------------- */
  280.  
  281. short TextsendSetup(
  282.     register Byte *prompt,
  283.     register long linedelay,
  284.     register long chardelay)
  285. {
  286.     if ((long)prompt) {
  287.         Byte s[256];
  288.         memcpy(s, prompt, *prompt + 1);
  289.         if (*s > sizeof(Settings.prompt) - 1)
  290.             *s = sizeof(Settings.prompt) - 1;
  291.         if (!EqualString(s, Settings.prompt, FALSE, FALSE)) {
  292.             memcpy(Settings.prompt, s, *s + 1);
  293.             Settings.dirty = TRUE;
  294.         }
  295.     }
  296.     if (linedelay >= 0 && linedelay != Settings.linedelay) {
  297.         Settings.linedelay = linedelay;
  298.         Settings.dirty = TRUE;
  299.     }
  300.     if (chardelay >= 0 && chardelay != Settings.chardelay) {
  301.         Settings.chardelay = chardelay;
  302.         Settings.dirty = TRUE;
  303.     }
  304.     return FINE;
  305. }
  306.  
  307. /* ----- Receive binary file ------------------------------------------- */
  308.  
  309. short Download(
  310.     register Byte *name,            /* File name */
  311.     register Boolean MacBinary,
  312.     short protocol)
  313. {
  314.     register short err;
  315.     register Boolean binary;
  316.  
  317.     binary = Settings.Binary;            /* Save MacBinary flag */
  318.     Settings.Binary = MacBinary;
  319.  
  320.     if (protocol == 1) {                /* ZModem */
  321.         err = ZReceive();
  322.         UnloadSeg(ZReceive);
  323.     } else {                            /* XYModem */
  324.         SerialBinary(Settings.portSetup);
  325.         DeleteFile(Settings.volume, Settings.directory, name);
  326.         err = XReceive(name, Settings.volume, Settings.directory);
  327.         UnloadSeg(XReceive);
  328.         SerialReset(Settings.portSetup);
  329.     }
  330.  
  331.     Settings.Binary = binary;            /* Restore MacBinary flag */
  332.     FlushVol(0, Settings.volume);
  333.     return err;
  334. }
  335.  
  336. /* ----- Transmit binary file ------------------------------------------ */
  337.  
  338. short Upload(
  339.     register Byte *name,            /* File name */
  340.     register Boolean MacBinary,
  341.     short protocol)
  342. {
  343.     register short err;
  344.     register Boolean binary;
  345.  
  346.     binary = Settings.Binary;            /* Save MacBinary flag */
  347.     Settings.Binary = MacBinary;
  348.  
  349.     if (protocol == 1) {                /* ZModem */
  350.         err = ZTransmit(name, Settings.volume, Settings.directory);
  351.         UnloadSeg(ZTransmit);
  352.     } else {                            /* XYModem */
  353.         SerialBinary(Settings.portSetup);
  354.         err = XTransmit(name, Settings.volume, Settings.directory);
  355.         UnloadSeg(XTransmit);
  356.         SerialReset(Settings.portSetup);
  357.     }
  358.  
  359.     Settings.Binary = binary;            /* Restore MacBinary flag */
  360.     return err;
  361. }
  362.